<!--网页打开自动加载-->
const vm = new Vue({
    el: "#app",
    data: {
        baseInfo: {
            tourInfo: "一、重渡沟开放时间\n" +
                "\n" +
                "旺季（4月1日-10月31日）：6:00开园，19:00停止入园，20:00闭园。\n" +
                "\n" +
                "淡季（11月1日-3月31日）：6:30开园，18:00停止入园，19:00闭园。\n" +
                "\n" +
                "二、景点院落开放时间\n" +
                "\n" +
                "旺季（4月1日-10月31日）：8:00开门，17:30停止进入，18:00关门。\n" +
                "\n" +
                "淡季（11月1日-3月31日）：8:30开门，16:30停止进入，17:00关门。",
            ticketInfo: "重渡沟门票80/人\n" +
                "\n" +
                "漂流:108元/人，垂钓:30元/小时，农耕村: 20元/人(暂免)\n" +
                "\n" +
                "滑草: 60元/人，滑道:15元/人，滑索:15元/人，悠玻球：10元/人\n" +
                "\n" +
                "上网:4元/小时，KTV：30元/小时 \n" +
                "重渡沟门票一票制，不限时\n" +
                "\n" +
                "免票政策：\n" +
                "\n" +
                "1.儿童票2.5折。\n" +
                "\n" +
                "2.团体票半价。\n" +
                "\n" +
                "3.军人、记者、导游凭证免票。河南省摄影家协会会员半价。"
        },
        btn: {
            btn_nav: "home",
            btn_user: "user_login",
            sub_btn_user: "buy_ticket",
            sub_btn_user_update: "select",
        },
        user: {
            user: {
                userNo: "",
                password: "",
            },
            register_user: {
                userNo: "",
                password: "",
            },
            new_contact: {
                id: null,
                name: null,
            },
            contacts: [],
        },
        ticket: {
            price_standards: [],
            new_ticket_select_contact: "",
            new_ticket_online: {
                ticketNo: 0,
                createDate: "",
                ticketType: "",
                id: ""
            },
            my_tickets: [],
        },
        today: "",
        max_day: "",    // 最晚可以预约的日期
        limit_days: 14, // 最大可预约时限
    },
    mounted() {
        // 初始化日期数据
        let now = new Date()
        this.today = now.toISOString().slice(0, 10)
        let future = new Date(now.getTime() + this.limit_days*24*60*60*1000)
        this.max_day = future.toISOString().slice(0, 10)
    },
    computed: {
        new_ticket_online_price: {
            get: function () {
                // 数组find函数+匿名箭头函数
                const standard = this.ticket.price_standards.find((standard) => {
                    return standard.ticketType === this.ticket.new_ticket_online.ticketType
                })
                if (standard)
                    return standard.price
                return 0
            }
        },
    },
    // watch: {
    //     // 监视ticketType属性，当更改选择的票种时更改价格
    //     "ticket.new_ticket_online.ticketType": {
    //         // 启动界面立即监视一次
    //         immediate: true,
    //         handler(newVal, oldVal) {
    //             this.ticket.new_ticket_online.price = this.new_ticket_online_price
    //         }
    //     },
    // },
    methods: {
        /**
         * 响应首页按钮
         * 本来是点击首页后会向后台申请购票及游览信息，但是为了简单，直接将其写道前端界面，不申请了
         */
        response_home_btn: function () {
            this.btn.btn_nav='home'
        },

        /**
         * 响应公告按钮
         * 暂时不实现公告功能，考虑后续有时间再把他写死到前端界面当壳子
         */
        response_notice_btn: function () {
            this.btn.btn_nav='notice'

            //请求全部公告信息
            this.request_notice()
        },

        /**
         * 请求公告信息
         * @param infoNo
         */
        notice_detail: function (infoNo) {
            // 显示公告细节区域
            this.btn.btn_nav='detail'

            // 异步申请公告信息
            this.request_notice_detail(infoNo)
        },

        user_login: function() {
            let userNo = this.user.user.userNo
            let password = this.user.user.password
            if (userNo === "" || password === "") {
                alert("账号或密码不能为空")
            }
            else {
                axios({
                    method: "post",
                    url: "http://localhost:7001/user-service/user/login",
                    data: this.user.user
                }).then(resp => {
                    if (resp.data === false) {
                        this.user.user.password = ""
                        alert("用户名或密码错误！")
                    }
                    else {
                        this.btn.btn_user = "login_success"
                        // 请求联系人数据
                        this.request_contacts()
                        // 请求票价标准数据
                        this.request_price_standards()
                    }
                })
            }
        },

        user_register: function () {
            let userNo = this.user.register_user.userNo
            let password = this.user.register_user.password

            if (userNo === "" || password === "") {
                alert("账号、昵称或密码不能为空")
            }
            else {
                axios({
                    method: "post",
                    url: "http://localhost:7001/user-service/user/register",
                    data: this.user.register_user
            }).then(resp => {
                    if (resp.data === true) {
                        alert("注册成功")
                        this.btn.btn_user = "user_login"

                        // 注册信息清空
                        this.user.register_user.userNo = ""
                        this.user.register_user.password = ""
                    }
                    else {
                        alert("账号已存在")
                    }
                })
            }
        },

        response_my_contacts: function () {
            this.btn.sub_btn_user='contact'
            this.btn.sub_btn_user_update='select'

            this.request_contacts();
        },

        response_return_add_contacts: function () {
            this.btn.sub_btn_user='contact'

            // 清空添加栏信息
            this.user.new_contact.ID = ""
            this.user.new_contact.teleNum = ""
        },

        response_delete_contact: function (ID) {
            if (confirm("是否删除该条联系人记录？")) {
                axios({
                    method: "post",
                    url: "http://localhost:8080/TicketManagementSystem/deleteContactLinkServlet",
                    data: {
                        _userNo: this.user.user.userNo,
                        _ID: ID
                    }
                }).then(resp => {
                    //重新请求联系人   由于axios请求异步进行，所以之后有关行为不能在axios外面
                    this.request_contacts()

                    alert("删除成功")
                })
            }
        },

        response_new_contact: function() {
            let ID = this.user.new_contact.id
            if (ID === "") {
                alert("请输入联系人身份证号")
            }
            else if (ID.length !== 18) {
                alert("身份证号不合法")
            }
            else {
                axios({
                    method: "post",
                    url: "http://localhost:7001/user-service/person/addPerson",
                    data: this.user.new_contact
                }).then(resp => {
                    axios({
                        method: "post",
                        url: "http://localhost:7001/user-service/person/addUp",
                        data: {
                            userNo: this.user.user.userNo,
                            id: this.user.new_contact.id
                        }
                    }).then(resp => {
                        if (resp.data === true) {
                            alert("添加成功！")
                            this.request_contacts()
                            this.btn.sub_btn_user = 'contact'

                            // 添加栏清空
                            this.user.new_contact.id = ""
                            this.user.new_contact.name = ""
                        }
                        else {
                            alert("该联系人已存在！")
                        }
                    })

                })
            }
        },

        response_buy_ticket: function () {
            this.btn.sub_btn_user='buy_ticket'

            // 请求数据
            this.request_contacts()
            this.request_price_standards()
        },

        response_my_ticket: function () {
            this.btn.sub_btn_user='my_ticket'

            // 请求数据
            this.request_my_tickets()
        },

        request_contacts: function () {
            axios({
                method: "post",
                url: "http://localhost:7001/user-service/person/person-all",
                data: {
                    userNo: this.user.user.userNo,
                    password: ""
                }
            }).then(resp => {
                this.user.contacts = resp.data
            })
        },

        request_my_tickets: function () {
            axios({
                method: "post",
                url: "http://localhost:7001/user-service/ticket/all",
                data: this.user.user
            }).then(resp => {
                this.ticket.my_tickets = resp.data

                // 门票按照日期降序
                this.ticket.my_tickets.sort((a, b) => {
                    if (a.createDate < b.createDate) {
                        return 1;
                    } else if (a.createDate > b.createDate) {
                        return -1;
                    } else {
                        return 0;
                    }
                })
            })
        },

        request_price_standards: function() {
            axios({
                method: "post",
                url: "http://localhost:7001/user-service/standard/selectAll",
            }).then(resp => {
                // 内置JSON序列化实现引用数据类型的深拷贝
                this.ticket.price_standards = JSON.parse(JSON.stringify(resp.data))
                this.ticket.changed_price_standards = JSON.parse(JSON.stringify(resp.data))
            })
        },

        submit_buy_ticket_online: function () {
            let time = this.ticket.new_ticket_online.createDate
            let ticketType = this.ticket.new_ticket_online.ticketType
            let select_contact = this.ticket.new_ticket_online.id
            if (time === "") {
                alert("请选择出行日期")
            }
            else if (ticketType === "") {
                alert("请选择购票类型")
            }
            else if(select_contact === "") {
                alert("请选择购票联系人")
            }
            else {
                axios({
                    method: "post",
                    url: "http://localhost:7001/user-service/ticket/add",
                    data: this.ticket.new_ticket_online
                }).then(resp => {
                    if (resp.data === true) {
                        // 购票界面数据清空
                        this.ticket.new_ticket_online.createDate = ""
                        this.ticket.new_ticket_online.ticketType = ""
                        this.ticket.new_ticket_online.id = ""

                        alert("购票成功")
                    }
                    else {
                        alert("购票失败")
                    }
                })
            }
        },

        soon: function () {
            alert("敬请期待")
        },
    }
})